package com.quzi.kettle.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.quzi.kettle.R;

public class StatusViewGroup extends ViewGroup{

	private static final int MAIN_TEXT = 0;
	private static final int UNIT = 1;
	private static final int SUB_TEXT1 = 2;
	private static final int SUB_TEXT2 = 3;
	
	private TextView mainText;
	private TextView unit;
	private TextView subText1;
	private TextView subText2;
	private SeekArc seekArc;
	
	private int mLeftWidth, mRightWidth;
	private Rect mTmpContainerRect = new Rect();
	private Rect mTmpChildRect = new Rect();
	
	private float centerX, centerY;
	private int progress = 0;
	
	private Handler UIHandler = new Handler(){
	@Override
		public void dispatchMessage(Message msg) {
			// TODO Auto-generated method stub
			super.dispatchMessage(msg);
			switch(msg.what){
			case 0:
				seekArc.setProgress(progress);
				progress ++;
				if(progress >= 100){
					progress = 0;
				}
				UIHandler.sendEmptyMessageDelayed(0, 100);
				break;
			}
		}	
	};
	
	public StatusViewGroup(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public StatusViewGroup(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public StatusViewGroup(Context context) {
		super(context);
		init();
	}
	
	private void init(){
		seekArc = new SeekArc(getContext());
		seekArc.setStartAngle(180);
		seekArc.setThumbEnable(false);
		seekArc.setFillHeight(140);
		seekArc.setProgressWidth(50);
		
		mainText = new TextView(getContext());
		mainText.setId(MAIN_TEXT);
		mainText.setText("0");
		mainText.setTextColor(getResources().getColor(R.color.orange));
		mainText.setTextSize(100);
		
		unit = new TextView(getContext());
		unit.setId(UNIT);
		unit.setText("℃");
		unit.setGravity(Gravity.RIGHT | Gravity.BOTTOM);
		unit.setTextColor(getResources().getColor(R.color.grey));
		unit.setTextSize(30);
		
		subText1 = new TextView(getContext());
		subText1.setId(SUB_TEXT1);
		subText1.setText("水量充足");
		subText1.setTextColor(getResources().getColor(R.color.white));
		subText1.setTextSize(25);
		
		subText2 = new TextView(getContext());
		subText2.setId(SUB_TEXT2);
		subText2.setText("加热中");
		subText2.setTextColor(getResources().getColor(R.color.white));
		subText2.setTextSize(30);
		
		
		addView(seekArc, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		addView(mainText, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		addView(unit, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		addView(subText1, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		addView(subText2, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		
//		UIHandler.sendEmptyMessage(0);
	}
	
	/**
	 * 更新UI
	 * @param mainText
	 * @param subText1
	 * @param subText2
	 */
	public void updateView(int mainText, String subText1, String subText2){
		this.mainText.setText(""+mainText);
		if(subText1 != null){
			this.subText1.setText(subText1);
		}
		if(subText2 != null){
			this.subText2.setText(subText2);
		}
		seekArc.setProgress(mainText);
		invalidate();
	}
	
	public void updateView(int mainText){
		updateView(mainText, null, null);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		
		int childState = 0;
		int childCount = getChildCount();
		
		setMeasuredDimension(seekArc.getWidth(), seekArc.getHeight());
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		for(int i=0;i<childCount;i++){
			View child = getChildAt(i);
			childState = combineMeasuredStates(childState, child.getMeasuredState());
		}
	
	
		int count = getChildCount();

        // These keep track of the space we are using on the left and right for
        // views positioned there; we need member variables so we can also use
        // these for layout later.
        mLeftWidth = 0;
        mRightWidth = 0;

        // Measurement will ultimately be computing these values.
        int maxHeight = 0;
        int maxWidth = 0;

        centerX = getWidth()/2;
        centerY = getHeight()/2;
        // Iterate through all children, measuring them and computing our dimensions
        // from their size.
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                // Measure the child.
                measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);

                // Update our size information based on the layout params.  Children
                // that asked to be positioned on the left or right go in those gutters.
                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
                if (lp.position == LayoutParams.POSITION_LEFT) {
                    mLeftWidth += Math.max(maxWidth,
                            child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
                } else if (lp.position == LayoutParams.POSITION_RIGHT) {
                    mRightWidth += Math.max(maxWidth,
                            child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
                } else {
                    maxWidth = Math.max(maxWidth,
                            child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
                }
                maxHeight = Math.max(maxHeight,
                        child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
                childState = combineMeasuredStates(childState, child.getMeasuredState());
            }
        }

        // Total width is the maximum width of all inner children plus the gutters.
        maxWidth += mLeftWidth + mRightWidth;

        // Check against our minimum height and width
        maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
        maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());

        // Report our final dimensions.
        setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
                resolveSizeAndState(maxHeight, heightMeasureSpec,
                        childState << MEASURED_HEIGHT_STATE_SHIFT));
	
	}
	

	@Override
	protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
		// TODO Auto-generated method stub
		final int count = getChildCount();

        // These are the far left and right edges in which we are performing layout.
        int leftPos = getPaddingLeft();
        int rightPos = right - left - getPaddingRight();

        // This is the middle region inside of the gutter.
        final int middleLeft = leftPos + mLeftWidth;
        final int middleRight = rightPos - mRightWidth;

        // These are the top and bottom edges in which we are performing layout.
        final int parentTop = getPaddingTop();
        final int parentBottom = bottom - top - getPaddingBottom();

        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                final LayoutParams lp = (LayoutParams) child.getLayoutParams();

                final int width = child.getMeasuredWidth();
                final int height = child.getMeasuredHeight();

                // Compute the frame in which we are placing this child.
                if (lp.position == LayoutParams.POSITION_LEFT) {
                    mTmpContainerRect.left = leftPos + lp.leftMargin;
                    mTmpContainerRect.right = leftPos + width + lp.rightMargin;
                    leftPos = mTmpContainerRect.right;
                } else if (lp.position == LayoutParams.POSITION_RIGHT) {
                    mTmpContainerRect.right = rightPos - lp.rightMargin;
                    mTmpContainerRect.left = rightPos - width - lp.leftMargin;
                    rightPos = mTmpContainerRect.left;
                } else {
                    mTmpContainerRect.left = middleLeft + lp.leftMargin;
                    mTmpContainerRect.right = middleRight - lp.rightMargin;
                }
                mTmpContainerRect.top = parentTop + lp.topMargin;
                mTmpContainerRect.bottom = parentBottom - lp.bottomMargin;

                // Use the child's gravity and size to determine its final
                // frame within its container.
                Gravity.apply(lp.gravity, width, height, mTmpContainerRect, mTmpChildRect);

                switch(child.getId()){
                case MAIN_TEXT:
                		child.layout((int)(centerX-width/2), (int)(centerY-height/1.3), (int)(centerX+width/2), (int)(centerY+height/1.3));
                	break;
                case UNIT:
                		child.layout((int)(centerX-width/2+230), (int)(centerY-height/2), (int)(centerX+width/2+230), (int)(centerY+height/2));
                	break;
                case SUB_TEXT1:
                		child.layout((int)(centerX-width/2), (int)(centerY-height/2+160), (int)(centerX+width/2), (int)(centerY+height/2+160));
                	break;
                case SUB_TEXT2:
                		child.layout((int)(centerX-width/2), (int)(centerY-height/2+250), (int)(centerX+width/2), (int)(centerY+height/2+250));
                	break;
                default:
                	 child.layout(mTmpChildRect.left, mTmpChildRect.top,
                             mTmpChildRect.right, mTmpChildRect.bottom);
                	break;
                }

               
            }
        }
	}
	
	public static class LayoutParams extends MarginLayoutParams {
		/**
		 * The gravity to apply with the View to which these layout parameters
		 * are associated.
		 */
		public int gravity = Gravity.TOP | Gravity.START;

		public static int POSITION_MIDDLE = 0;
		public static int POSITION_LEFT = 1;
		public static int POSITION_RIGHT = 2;

		public int position = POSITION_MIDDLE;

		public LayoutParams(Context c, AttributeSet attrs) {
			super(c, attrs);
			TypedArray a = c.obtainStyledAttributes(attrs,
					R.styleable.CustomLayoutLP);
			gravity = a.getInt(
					R.styleable.CustomLayoutLP_android_layout_gravity, gravity);
			position = a.getInt(R.styleable.CustomLayoutLP_layout_position,
					position);
			a.recycle();
		}

		public LayoutParams(int width, int height) {
			super(width, height);
		}

		public LayoutParams(ViewGroup.LayoutParams source) {
			super(source);
		}
	}
	
	
	

}
